package com.irun.pm.ui;

import java.io.File;
import java.util.concurrent.Callable;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.irun.pm.R;
import com.irun.pm.controller.DriverInfoControllers;
import com.irun.pm.interfaces.Callback;
import com.irun.pm.mode.DriverInfo;
import com.irun.pm.utils.FileUtils;
import com.irun.pm.utils.ImageUtil;
import com.irun.pm.utils.StringUtil;
import com.irun.pm.utils.TakePhotoUtils;
import com.irun.pm.utils.Utils;
/**
 * 人员信息添加页面
 * @author Administrator
 *
 */
public class PopulationAddActivity extends BaseActivity implements OnClickListener{
	private DriverInfoControllers controllers;
	private EditText etName;
	private EditText etSex;
	private EditText etAge;
	private EditText etCard;
	private EditText etDriverNum;
	private EditText etAddress;
	private EditText etNative;
	//private EditText etDriverAge;
	private EditText etNotes;
	private ImageView ivCardPhoto;
	private static final File PHOTO_DIR = new File(Environment
			.getExternalStorageDirectory()
			+ "/DCIM/Camera");
	private static final int REQUES_CAMERA = 0;
	private static final int DIALOG_EXIT = 1;
	private File mCurrentPhotoFile;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.population_add);
		findViewById(R.id.btnSaveEdit).setOnClickListener(this);
		controllers = new DriverInfoControllers(this);
		etName = (EditText)findViewById(R.id.etName);
		etAge = (EditText)findViewById(R.id.etAge);
		etCard = (EditText)findViewById(R.id.etCard);
		etDriverNum = (EditText)findViewById(R.id.etDriverNum);
		etAddress = (EditText)findViewById(R.id.etAddress);
		etNative = (EditText)findViewById(R.id.etNative);
		//etDriverAge = (EditText)findViewById(R.id.etDriverAge);
		etNotes = (EditText) findViewById(R.id.etNotes);
		etSex = (EditText)findViewById(R.id.etSex);
		ivCardPhoto = (ImageView)findViewById(R.id.ivCardPhoto);
		ivCardPhoto.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnSaveEdit:
			final String name = etName.getText().toString().trim();
			String card = etCard.getText().toString().trim();
			if(StringUtil.isEmpty(name)){
				etName.setError(Html
						.fromHtml("<font  color='black'>"+"请输入名称"+"</font>"));
				return;
			}else if(StringUtil.isEmpty(card)){
				etCard.setError(Html
						.fromHtml("<font  color='black'>"+"请输入身份证"+"</font>"));
				return;
			}
			doProgress("提示", "正在保存中...", new Callable<String>() {
				@Override
				public String call() throws Exception {
					// TODO Auto-generated method stub
					Thread.sleep(2000);
					return null;
				}
			}, new Callback<String>() {
				@Override
				public void onCallback(String pCallbackValue) {
					// TODO Auto-generated method stub
					try{
						
						DriverInfo driverInfo = new DriverInfo();
						driverInfo.name = name;
						driverInfo.sex = etSex.getText().toString().trim();
						driverInfo.card = etCard.getText().toString().trim();
						String age  = etAge.getText().toString().trim();
						driverInfo.note = etNotes.getText().toString();
						if(!StringUtil.isEmpty(age)){
							driverInfo.age = Integer.parseInt(age);
						}
						//driverInfo.driverAge = etDriverAge.getText().toString().trim();
						driverInfo.type = Utils.TYPE_PEOPLE_QUERY;	
						driverInfo.address = etAddress.getText().toString().trim();
						driverInfo.nativeAddress = etNative.getText().toString().trim();
						driverInfo.cardNumber = etDriverNum.getText().toString();
						driverInfo.photoPath = filePath;
						long result = controllers.insertDriverInfo(driverInfo, Utils.TYPE_PEOPLE_QUERY);
						if(result>0){
							Toast.makeText(getBaseContext(), "后台数据库无法连接，已成功暂存在本地!", Toast.LENGTH_SHORT).show();
							setResult(RESULT_OK);
							finish();
						}else{
							Toast.makeText(getBaseContext(), "添加失败", Toast.LENGTH_SHORT).show();
						}
					}catch (Exception e) {
						
					}
				}
			}, null);
			
			
			
			
			break;
		case R.id.ivCardPhoto:
			TakePhotoUtils takePhotoUtils = new TakePhotoUtils(this);
			PHOTO_DIR.mkdirs();
			mCurrentPhotoFile = new File(PHOTO_DIR, TakePhotoUtils.getPhotoFileName());
			Intent intent = takePhotoUtils.doTakePhoto( mCurrentPhotoFile);
			startActivityForResult(intent, REQUES_CAMERA);
			break;

		default:
			break;
		}
	}
	
	String filePath = "";
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == RESULT_CANCELED){
			return;
		}
		switch (requestCode) {
		case REQUES_CAMERA:
			Bitmap bmp = ImageUtil.compressHeadBitMap(mCurrentPhotoFile.getAbsolutePath());
			filePath = FileUtils.FILE_POLECE_IMAGE+TakePhotoUtils.getPhotoFileName();
			ImageUtil.saveBmp(bmp, FileUtils.FILE_POLECE_IMAGE+TakePhotoUtils.getPhotoFileName());
			ivCardPhoto.setImageBitmap(bmp);
			break;

		default:
			break;
		}
	}
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		showDialog(DIALOG_EXIT);
//		super.onBackPressed();
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		AlertDialog dialog = null;
		switch (id) {
		case DIALOG_EXIT:
			dialog = new AlertDialog.Builder(this).setTitle("提示").setMessage("是否退出编辑?")
			.setPositiveButton("确定", new Dialog.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub
					PopulationAddActivity.this.finish();
				}
			}).setNegativeButton("取消", null).create();
			break;

		default:
			break;
		}
		return dialog;
	}
}
